package au.edu.jcu.it.numberguess;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import android.app.Application;
import android.media.MediaPlayer;

public class NumberGuessApp extends Application {
	protected GameSettings gameSettings;
	protected CardSamples cardSamplesModel;
	protected boolean backFromSetting;
	protected Vector<Integer> listGuess;
	protected int appSerectNumber;
	private String userName;
	protected int playerScore;
	protected int appScore;
	protected long lTimer = 0;
	private int lPause;
	protected MediaPlayer mediaPlayer;

	@Override
	public void onCreate() {
		super.onCreate();
		gameSettings = new GameSettings();
		cardSamplesModel = new CardSamples();
		backFromSetting = false;
		listGuess = new Vector<Integer>();
		setListGuess();
		setAppSecrectNumer();
		mediaPlayer = MediaPlayer.create(getApplicationContext(),
				R.raw.guess_the_number);
		mediaPlayer.setLooping(true);
	}

	public List<Integer> createList() {
		List<Integer> tempList = new ArrayList<Integer>();

		if (gameSettings.getCardMode().trim().equals("BINARY"))

			tempList = cardSamplesModel.binaryCreate(gameSettings
					.getRangeNumber());

		else {
			if (gameSettings.getCardMode().trim().equals("PRIME"))

				tempList = cardSamplesModel.primeCreate(gameSettings
						.getRangeNumber());

			else

				tempList = cardSamplesModel.fibonaciCreate(gameSettings
						.getRangeNumber());

		}

		return tempList;
	}

	public void setCardSamples(Integer integer, List<Integer> tempList) {
		Integer count = 0;
		while (count < tempList.size()) {
			cardSamplesModel.add(new Card());
			count++;
		}
		int z;
		for (int i = 1; i <= integer; i++) {
			z = i;
			for (int j = tempList.size() - 1; j >= 0; j--) {
				if (z / tempList.get(j) == 1) {

					if (cardSamplesModel.get(j).indexOf(i) == -1) {
						cardSamplesModel.get(j).add(i);

					}
					z = z - tempList.get(j);
				}
			}

		}

	}

	public void Refresh() {
		playerScore = 0;
		lTimer = 0;
		appScore = 0;
		setListGuess();
		cardSamplesModel.clear();
		List<Integer> list = new ArrayList<Integer>();
		list = createList();
		setCardSamples(gameSettings.getRangeNumber(), list);
	}

	public void appGuess(String string, Card listNumber) {
		String str = "app GUESS:";
		if (string.trim().equals("NO")) {
			for (int j = 0; j < listNumber.size(); j++)
				for (int k = 0; k < listGuess.size(); k++) {
					if (listGuess.elementAt(k) == listNumber.get(j))
						listGuess.remove(k);
				}
		} else {
			for (int k = 0; k < listGuess.size(); k++)

			{
				int iEqual = 0;
				for (int j = 0; j < listNumber.size(); j++) {

					if (listGuess.elementAt(k) == listNumber.get(j))
						iEqual = 1;
				}
				if (iEqual == 0)
					listGuess.remove(k);
			}

		}

		for (int z = 0; z < listGuess.size(); z++) {
			str = str + listGuess.elementAt(z).toString() + ";";
		}
		System.out.println(str);
	}

	public void setListGuess() {
		listGuess.clear();
		for (int i = 1; i <= gameSettings.getRangeNumber(); i++)
			listGuess.add(i);
	}

	public void setAppSecrectNumer() {
		Random r = new Random();
		appSerectNumber = r.nextInt(gameSettings.getRangeNumber()) + 1;
	}

	public int getAppSecretNumber() {
		return appSerectNumber;
	}

	@Override
	public void onLowMemory() {

		super.onLowMemory();
		cardSamplesModel.clear();
		mediaPlayer.release();
	}

	@Override
	public void onTerminate() {

		super.onTerminate();
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getlPause() {
		return lPause;
	}

	public void setlPause(int lPause) {
		this.lPause = lPause;
	}

}
